<?php
/*
通用分类
*/
define('Acache_no','1');
class Con_area extends Acan{
	public $defaultAct='manage';
	public $Conread = '管理';
	public $Actread = array('manage'=>'配置管理','save'=>'保存','del'=>'删除','delall'=>'删除所有');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->model = Afun_db()->model(Acan_table_area);
	}
	function Act_build_js(){
		if($this->id<1){
			$this->id = 1;
		}
		$post = $_POST;
		if($post){
			$area = Afunc_area()->getdata($post['level'],$post['since']);
			//print_r($area);
			$data = 'Acan.area = '.Ajson_encode($area);
			$path = A_ROOT.'js/data/area'.$post['level'].$post['since'].'.js';
			file_put_contents($path,$data);
			echo "创建地区 $path 成功";
		}
		include $this->Atpl();
	}
	//地区导航html
	function nav_html($pid){
		$obj = $this->model->ofindPk($pid);
		if($obj->pid>0){
			$html = $this->nav_html($obj->pid);
		}elseif($obj->pid==0){
			$html = '<a href="'.$this->Aactr.'-0">所有</a> '.$html;
		}
		$html .= '> <a href="'.$this->Aactr.'-'.$obj->id.'">'.$obj->name.'</a> ';
		return $html;
	}
	//--用户管理
	function Act_manage(){
		if(empty($this->atitle)){$this->atitle = '配置管理';}
		$obj = Afun_acan_new('list');
		$key_name = $this->model->key_name;
		$columnrr = $this->model->comment;//初始栏目
		unset($columnrr['appid']);//密码栏目不显示
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$column_html = $obj->column_html($this->model,$column,'Aform');
		$column_json = $obj->form_json($this->model);
		//分页函数
		$con->where = "1";
		$nav_html = $this->nav_html($this->id);
		$pobj = $this->model->ofindPk($this->id);
		$addform = json_encode(array('pid'=>$this->id,'class'=>$pobj->class+1));
		if($this->id>0){
			$con->where .= " and a.pid=".$this->id;
		}else{
			$con->where .= " and a.pid=0";
		}
		$con->select = "a.*";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->page = '20';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->findAll($con);
		include $this->Atpl();
	}
	//--获取单用户信息
	function Act_info(){
		if($this->id<1){
			return;
		}
		$info = $this->model->ofindPk($this->id);
		Afun_json($info);
	}
	//批量更新
	function Act_save(){
		Afun_post_auth();
		$savedata = $_POST['save'];
		if($this->id == 'one'){
			$key = $savedata[$this->model->key_name];
			unset($savedata[$this->model->key_name]);
			if(!empty($key)){
				$msg = $this->model->updatePk($savedata,$key);
			}else{//--添加一个新用户
				$savedata['appid'] = Acan_app_id;
				$msg = $this->model->save($savedata);
			}
			Afunc_option($savedata['key'])->delcache();
		}elseif(is_array($savedata)){
			foreach($savedata as $i=>$vals){
				$this->model->updatePk($vals,$vals['key']);
				Afunc_option($vals['key'])->delcache();
			}
		}
		Afun_json($msg);
	}
	//删除
	function Act_del(){
		Afun_post_auth();
		if($this->model->deletePk($this->id)){
			Afunc_group()->usergroupdel($this->id);
			echo '删除成功';
		}
	}
	//删除所选
	function Act_delall(){
		Afun_post_auth();
		foreach($_POST['chk'] as $key=>$val){
			Afunc_group()->usergroupdel($val);
			if(empty($vala))
				$vala = "'$val'";
			else
				$vala .=",'$val'";
		}
		$this->model->delete($this->model->key_name." IN ($vala)");
		Afun_refresh("删除成功",'referrer');
	}
	

}

?>